package ray.mgocc.net;

import ray.mgocc.Source;
import java.io.InputStream;
import java.io.IOException;
import org.apache.commons.httpclient.HttpMethod;

public class Response implements Source{
    private HttpMethod method;
    /**
     * 実行後のrequest.
     */
    protected Response(HttpMethod method){
        if(!method.isRequestSent()){
            throw new RuntimeException("bug.method did not sent."+method);
        }
        this.method = method;
    }
    public void finalize(){
        close();
    }
    private HttpMethod getMethod(){
        return this.method;
    }
    public void close(){
        HttpMethod method = getMethod();
        if(null!=method){
            method.releaseConnection();
        }
    }
    public InputStream getStream(){
        InputStream in = null;
        try{
            in = getMethod().getResponseBodyAsStream();
        }catch(IOException e){
            throw new RuntimeException("bug.",e);
        }
        return in;
    }
}
